package cn.hnu.qq.client.panel;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import cn.hnu.qq.client.controller.CenterController;
import cn.hnu.qq.client.controller.FriendsListController;
import cn.hnu.qq.client.controller.ListenClientMessageThread;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.frame.ClientFrame;
import cn.hnu.qq.frame.CrowdChartDialog;
import cn.hnu.qq.frame.SingleChartDialog;
import cn.hnu.qq.global.MessageType;

/**
 * 好友列表面板
 * @author jared
 *
 */
public class FriendsListPanel extends JPanel implements   MouseListener,MessageListener{

	private JList<User> friendsList = new JList<User>();
	
	private JList<Crowd> corwdsList = new JList<Crowd>();;
	
	private JTabbedPane tabbedPane;
	
	private FriendsListController controller = FriendsListController.getInstance();

	private int index = 0;
	
	private SingleChartPanel panel = null;
	/**
	 * Create the panel.
	 */
	public FriendsListPanel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 192, 80);
		add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JLabel acountLabel = new JLabel("account");
		panel.add(acountLabel, BorderLayout.NORTH);
		
		JLabel imageLabel = new JLabel("image");
		panel.add(imageLabel, BorderLayout.WEST);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel infoLabel = new JLabel("info");
		infoLabel.setBounds(10, 24, 118, 15);
		panel_1.add(infoLabel);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 100, 193, 356);add(tabbedPane);
		
		friendsList.addMouseListener(this);
		corwdsList.addMouseListener(this);
		tabbedPane.add(friendsList, 0);
		tabbedPane.setTitleAt(0, "好友列表");
		tabbedPane.add(corwdsList,1);
		tabbedPane.setTitleAt(1, "群组列表");
		
		this.setSize(213,480);
		setFriendsList(controller.getCurUser().getFriends());
		setCorwdsList(controller.getCurUser().getCrowds());
		acountLabel.setText(controller.getCurUser().getAcount());
		
		controller.getClient().refreshPanel(this);
		
		//添加消息机制
		ListenClientMessageThread.getInstance().addMessageListener(this);
		//初始化 单聊对话框，但不显示
		SingleChartDialog.getInstance();
		//群聊
		CrowdChartDialog.getIntance();
	}

	
	public void setFriendsList(Set<User> users) {
		DefaultListModel<User> model = new DefaultListModel<User>();
		for(User user : users){
			model.addElement(user);
		}
		friendsList.setModel(model);
	}


	public void setCorwdsList(Set<Crowd> corwds) {
		DefaultListModel<Crowd> model = new DefaultListModel<Crowd>();
		for(Crowd Crowd : corwds){
			model.addElement(Crowd);
		}
		corwdsList.setModel(model);
	}


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientFrame window = new ClientFrame();
					window.refreshPanel(new FriendsListPanel());
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()%2 == 0){
			if(tabbedPane.getSelectedIndex() == 0){
				JOptionPane.showMessageDialog(controller.getClient(), "ffff");
				User user = friendsList.getSelectedValue();
				if(controller.getSingDialog() == null){
					CenterController.setSingDialog(SingleChartDialog.getInstance());
				}
				if(!controller.getSingDialog().isVisible()){
					controller.getSingDialog().setVisible(true);
				}
				controller.getSingDialog().createChartTab(user);
			}
			if(tabbedPane.getSelectedIndex() == 1){
				JOptionPane.showMessageDialog(controller.getClient(), "cccc");
				Crowd crowd = corwdsList.getSelectedValue();
				if(controller.getCrowdDialog() == null){
					CenterController.setCrowdDialog(CrowdChartDialog.getIntance());
				}
				if(!controller.getCrowdDialog().isVisible()){
					controller.getCrowdDialog().setVisible(true);
				}
				controller.getCrowdDialog().createChartTab(crowd);
			}
		}
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.CHART){
			SingleChartDialog scd = SingleChartDialog.getInstance();
			if(scd.isVisible() == false){
				scd.setVisible(true);
			}
		}
		if(message.getMessageType() == MessageType.CROWD_CHART){
			CrowdChartDialog ccd = CrowdChartDialog.getIntance();
			if(ccd.isVisible() == false){
				ccd.setVisible(true);
			}
		}
	}


}
